const addon = require('./majiang');
const mjObj = new addon.Majiang(); 

/*
 * Majiang Object方法:
 * 1:胡牌校验
 *      bool isTing(Array,lzNumber,need258)
 *      参数说明:
 *      Array:数组,不包含癞子的麻将牌
 *      lzNumber:数值,要校验的牌中的癞子数
 *      need258:布尔值,是否要258做将,true:要,false:不要(默认)
 *      返回值:布尔数据,true:可胡,false:不能胡
 *      说明:(Array.Length()+lzNumber)%3==2,否则返回数据有问题
 * 2:听牌校验
 *      Array isTing(Array,lzNumber,need258)
 *      参数说明:
 *      Array:数组,不包含癞子的麻将牌
 *      lzNumber:数值,要校验的牌中的癞子数
 *      need258:布尔值,true:要258做将,false:不要(默认)
 *      返回值:数组,可以胡的牌
 *      说明:(Array.Length()+lzNumber)%3==1,否则返回数据有问题
 */

const cards=[
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,  ///万
    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,  ///条
    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,  ///筒

    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,  ///万
    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,  ///条
    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,  ///筒

    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,  ///万
    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,  ///条
    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,  ///筒

    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,  ///万
    0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,  ///条
    0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,  ///筒
]


/*
 * 生成一定范围随机整数
 */
function random(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

/*
 * 在array中随机取count个元素
 */
function getRandomArrayElements(arr, count) {
    var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
    while (i-- > min) {
        index = Math.floor((i + 1) * Math.random());
        temp = shuffled[index];
        shuffled[index] = shuffled[i]; 
        shuffled[i] = temp;
    }
    return shuffled.slice(min);
}

/*
 * 麻将牌显示为16进制数据
 */
function toHexString(arr){
    let hexString="";
    for(let i=0;i<arr.length;i++){
        if(arr[i]<16){
            hexString+="0x0";
        }else{
            hexString+="0x";
        }
        hexString+=arr[i].toString(16);
        hexString+=",";
    }
    return hexString;
}


function check() {
    let cc0=[0x04,0x08,0x12,0x17,0x19,0x26];
    let r0 = mjObj.isting(cc0, 1);
    console.log("听:"+r0);

    // let cc1=[0x3,0x5,0x6,0x7,0x8,0x11,0x14,0x27,0x28];
    // let r1 = mjObj.isting(cc1, 1);
    // console.log("听:"+r1);

    // let cc2=[0x3,0x5,0x6,0x7,0x8,0x11,0x14,0x27,0x28];
    // let r2 = mjObj.ishu(cc2, 5);
    // console.log("胡:"+r2);

    // let cc3=[0x3,0x5,0x6,0x7,0x8,0x11,0x14,0x27,0x28];
    // let r3 = mjObj.ishu(cc3, 2);
    // console.log("胡:"+r3);

    // let cc4=[0x3,0x04,0x5,0x6,0x7,0x8,0x11,0x11,0x27,0x28,0x29];
    // let r4 = mjObj.ishu(cc4, 0);
    // console.log("胡:"+r4);

    // let cc5=[0x3,0x04,0x5,0x6,0x7,0x8,0x11,0x11];
    // let r5 = mjObj.ishu(cc5, 0);
    // console.log("胡:"+r5);

    console.log("      --->>> 胡牌校验 <<<---");
    for (var i = 0; i < 100000; i++) {
        //随机牌4-14张牌,癞子0-8张
        let cn = random(4, 14);
        let lz = random(0, 8);
        while (((lz + cn) % 3) != 2 || (lz + cn) > 14) {
            lz = random(0, 8);
        }
        let cc = getRandomArrayElements(cards, cn);
        //排序
        cc.sort(function(a,b){
            return a-b;
        });
        console.log("牌:" + toHexString(cc) + ",癞子数:" + lz);
        let r = mjObj.ishu(cc, lz);
        console.log("           胡:" + r);
    }

    // console.log("")
    // console.log("")
    // console.log("")
    // console.log("")
    console.log("      --->>> 听牌校验 <<<---");
    for (var i = 0; i < 100000; i++) {
        //随机牌4-14张牌,癞子0-8张
        let cn = random(4, 13);
        let lz = random(0, 8);
        while (((lz + cn) % 3) != 1 || (lz + cn) > 13) {
            lz = random(0, 8);
        }
        let cc = getRandomArrayElements(cards, cn);
        //排序
        cc.sort(function(a,b){
            return a-b;
        });
        console.log("牌:" + toHexString(cc) + ",癞子数:" + lz);
        let r = mjObj.isting(cc, lz);
        console.log("           听:" + toHexString(r));
    }

}

check();